package com.z100h.supplychain.workflow.facade;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.z100h.supplychain.purchase.entity.PurchaseOrder;
import com.z100h.supplychain.purchase.service.PurchaseOrderService;
import com.z100h.supplychain.workflow.entity.ActFlow;

/**
 * 采购单流程开发service
 */
@Service
@Transactional(readOnly = true)
public class PurchaseOrderFacadeServiceImpl {

	private static final Logger logger = LoggerFactory.getLogger(PurchaseOrderFacadeServiceImpl.class);

	/**
	 * 注入工作流的包
	 */
	@Autowired
	protected TaskService taskService;

	@Autowired
	protected HistoryService historyService;

	@Autowired
	protected RepositoryService repositoryService;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private IdentityService identityService;

	/**
	 * 定义流程信息
	 */
	private static String businessKey = "SC_PURCHASE";

	private static String businessTitle = "采购单";

	private static String businessType = "purchase";

	private static String businessUrl = "";

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	@Transactional(readOnly = false)
	public void startPurchaseOrder(ActFlow actFlow) {

		PurchaseOrder purchaseOrder = purchaseOrderService.get(actFlow.getBusinessId());
		User logUser = UserUtils.getUser();
		identityService.setAuthenticatedUserId(logUser.getLoginName());

		Map<String, Object> variables = new HashMap<>();

		variables.put("type", "purchase");
		variables.put("busId", purchaseOrder.getId());
		variables.put("title", "");// TODO 没有采购单编号 流程设置引擎id 需要重新定义

		// 启动流程
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(businessKey, variables);

		purchaseOrder.setDefinitionId(processInstance.getId());
		purchaseOrder.setUpdateBy(logUser);
		purchaseOrder.setUpdateDate(new Date());
		purchaseOrder.setDefinitionId(processInstance.getProcessDefinitionId());
		purchaseOrder.setInstanceId(processInstance.getId());

		purchaseOrderService.save(purchaseOrder);

	}

	/*
	 * @Transactional(readOnly = false) public void startPurchaseOrder(ActFlow actFlow) { PurchaseOrder purchaseOrder =
	 * purchaseOrderService.get(actFlow.getBusinessId()); User logUser = UserUtils.getUser();
	 * identityService.setAuthenticatedUserId(logUser.getLoginName()); Map<String, Object> variables=new HashMap<>();
	 * variables.put("type", "purchase"); variables.put("busId", purchaseOrder.getId());
	 * variables.put("title",purchaseOrder.getPurchaseOrderNo());//设置标题； // 启动流程 ProcessInstance processInstance =
	 * runtimeService.startProcessInstanceByKey(businessKey, variables);
	 * purchaseOrder.setDefinitionId(processInstance.getId()); purchaseOrder.setUpdateBy(logUser);
	 * purchaseOrder.setUpdateDate(new Date()); purchaseOrder.setDefinitionId(processInstance.getProcessDefinitionId());
	 * purchaseOrder.setInstanceId(processInstance.getId()); purchaseOrderService.save(purchaseOrder); }
	 */

}
